
package org.soekrat.engine.pitbul.parser;

import java.io.IOException;

import org.soekrat.engine.forest.Tree;

public class TreeFormParser 
{
  public TreeFormParser(Tree parsed) throws IOException
  {
    for (Tree tree : parsed) 
    {
      String token=tree.getName();
      if (token.equals("{"))
      {
        int n=tree.getChildCount();
        if (n>0)
        {
          Tree formNode=tree.getChild(0);
          token=formNode.getName();
          if (token.equals("tree"))
          {
            System.out.println("make tree");
            
            Tree created=createTree(tree);
            
            System.out.print("created: "+created);
          }
        }
      }
    }
    
  }
  
  private Tree createTree(Tree tree) throws IOException
  {
    int n=tree.getChildCount();
    if (n<4)
    {
      throw new IOException("to few tokens");
    }
    
    String treeToken=tree.getChild(0).getName();
    
    String beToken=tree.getChild(1).getName();
    String nameToken=tree.getChild(2).getName();
    String cueToken=tree.getChild(3).getName();
    
    if (!treeToken.equals("tree"))
    {
      throw new IOException("expected tree found "+treeToken);
    }
    if (!beToken.equals("*be"))
    {
      throw new IOException("expected *be found "+beToken);
    }
    if (!cueToken.equals("#up") && !cueToken.equals("#down"))
    {
      throw new IOException("expected #up or #down found "+cueToken);
    }
    
    Tree retVal=new Tree(nameToken);
    
    return retVal;
  }
  
}
